package com.zhang.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zhang.cloud.service.FeignHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "GlobalFallback")//设置全局fallback，这样@HystrixCommand注解的方法都会fallback这个方法
public class FeignHystrixController {

    @Resource
    private FeignHystrixService  feignHystrixService;

    @GetMapping(value = "/fh/ok/{id}")
    public String pay_ok(@PathVariable("id")Integer id)
    {
        String res = feignHystrixService.paymentInfo_ok(id);
        return res;
    }
    @GetMapping(value = "/fh/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "TimeOutHandler",commandProperties = {
//            //这里规定3s以内就是正常的业务逻辑
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value="1500")
//    })
    @HystrixCommand
    public String pay_timeout(@PathVariable("id")Integer id)
    {
        String res = feignHystrixService.paymentInfo_timeout(id);
        return res;
    }

    public String TimeOutHandler(@PathVariable("id")Integer id){
        return "我是消费者80，8001请求正忙，请稍等~,或者自己运行出错，请检查自己";
    }


    public String GlobalFallback(){
        return "我这里是全局兜底方法";
    }



    @GetMapping(value = "/fh/circuit/{id}")
    public String circuit_test(@PathVariable("id")Integer id)
    {
        String res = feignHystrixService.paymentCircuitBreaker(id);
        return res;
    }


}
